<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$joined_id = $route->getByNum(5);

$joined = $db->getRow("select `J`.*,`R`.`user_id` from `joined` as `J`,`reports` as `R` where `J`.`joined_id` = '".$joined_id."' and `J`.`report_id` = `R`.`report_id` limit 1");

if (!$joined || $joined['user_id'] != $user->getUserId())
	goToPage("/coordinator/reports/look");

if (isset($_POST['type']) && $_POST['type'] == "post")
{
	$date = UV::readMQ($_POST['date']);
	$time = UV::readMQ($_POST['time']);
	$sname = UV::readMQ($_POST['sname']);
	$name = UV::readMQ($_POST['name']);
	$tname = UV::readMQ($_POST['tname']);
	$login = UV::readMQ($_POST['login']);
	$domain = UV::readMQ($_POST['domain']);
	$sex = UV::readMQ($_POST['sex']);
	$age = UV::readInt($_POST['age']);
	$city = UV::readInt($_POST['city']);
	$area = UV::readInt($_POST['area']);
	if (!$area)
		$area = 0;
	$street = UV::readMQ($_POST['street']);
	$comment = UV::readMQ($_POST['comment']);
	$date = strtotime($date." ".$time);
	
	$report = $db->sendQuery("update `joined` set `date`='$date',`sname`='$sname',`name`='$name',`tname`='$tname',`email`='".$login."@".$domain.".ru',`sex`='$sex',`age`='$age',`city`='$city',`area`='$area',`street`='$street',`comment`='$comment' where `joined_id` = '$joined_id' limit 1");
	
	require_once COORDINATOR.DS."head.php";
	if (!$report)
		echo "Ошибка! Проверьте введенные данные<br />";
	else
		goToPage("/coordinator/reports/look");
}
else
{
	require_once COORDINATOR.DS."head.php";
	?>
	<script type='text/javascript'>
	$(document).ready(function(){
		$("#editForm").submit(function(){
			var date = /^[0-9]{2}\.[0-9]{2}\.201[1-2]{1}$/;
			var time = /^[0-9]{2}:[0-9]{2}$/;
			
			if (!date.test($("#date").val()))
			{
				makeRed($("#date"));
				return false;
			}
			if (!time.test($("#time").val()))
			{
				makeRed($("#time"));
				return false;
			}
			return confirm("Редактировать отчет?");
		});
		
		function makeRed(obj){
			obj.css("border","1px solid red").focus();
		}
		
		$(".enter").keydown(function(){
			$(this).css("border","");
		})
		
		$("#city").change(function(){
			$.ajax({
					url: "/coordinator/getarea?city="+$(this).val(),
					success: function(data){
						$("#area option").remove();
						if (data != "")
						{
							cities = $.parseJSON(data);
							for (var i=0; i<cities.length; i++)
								$("#area").append("<option value='"+cities[i].area_id+"'>"+cities[i].name+"</option>");
						}
					}
			})
		});
	})
	</script>
	<form method="POST" id='editForm'>
		<input type='hidden' name='type' value='post' />
		<table cellpadding='0' cellspacing='5' style='width:600px;' border='0'>
			<tr>
				<td width="200px"><label for="date">Дата</label></td>
				<td width="400px"><input type='text' name='date' id='date' class="enter date" value='<?php echo date("d.m.Y",$joined['date']); ?>' /></td>
				<td width="100px"><label for="time">Время</label></td>
				<td width="500px"><input type='text' name='time' id='time' class="enter time" value='<?php echo date("H:i",$joined['date']); ?>' /></td>
			</tr>
			<tr>
				<td colspan='2' class="hint"><span>Формат: 01.01.2011</span></td>
				<td colspan='2' class="hint"><span>Формат: 12:00</span></td>
			</tr>
			<tr>
				<td><label for="sname">Фамилия</label></td>
				<td colspan='3'><input type='text' name='sname' id='sname' class="enter" value='<?php echo $joined['sname']; ?>' /></td>
			</tr>
			<tr>
				<td><label for="name">Имя</label></td>
				<td colspan='3'><input type='text' name='name' id='name' class="enter" value='<?php echo $joined['name']; ?>' /></td>
			</tr>
			<tr>
				<td><label for="tname">Отчество</label></td>
				<td colspan='3'><input type='text' name='tname' id='tname' class="enter" value='<?php echo $joined['tname']; ?>' /></td>
			</tr>
			<tr>
				<td><label for="login">E-mail</label></td>
				<td colspan='2'><input type='text' name='login' id='login' class="enter" value='<?php $email = explode("@",$joined['email']); echo $email[0]; ?>' /></td>
				<td>
					<select name='domain' id='domain'>
						<option value='mail' <?php if ($email[1] == "mail.ru") echo "selected"; ?>>@mail.ru</option>
						<option value='inbox'<?php if ($email[1] == "inbox.ru") echo "selected"; ?>>@inbox.ru</option>
						<option value='bk'<?php if ($email[1] == "bk.ru") echo "selected"; ?>>@bk.ru</option>
						<option value='list'<?php if ($email[1] == "list.ru") echo "selected"; ?>>@list.ru</option>
					</select>
				</td>
			</tr>
			<tr>
				<td><label for="sex">Пол</label></td>
				<td>
					<select name='sex' id='sex'>
						<option value='undefined' <?php if ($joined['sex'] == "undefined") echo "selected"; ?>>не известно</option>
						<option value='male' <?php if ($joined['sex'] == "male") echo "selected"; ?>>мужской</option>
						<option value='female' <?php if ($joined['sex'] == "female") echo "selected"; ?>>женский</option>
					</select>
				</td>
				<td><label for="age">Возраст</label></td>
				<td><input type='text' name='age' id='age' class="enter" style="width:50%" value='<?php echo $joined['age']; ?>' /> лет</td>
			</tr>
			<tr>
				<td><label for="city">Город</label></td>
				<td>
					<select name='city' id='city'><?php
						$oren = $db->getCell("select `city_id` from `cities` where `name` like 'Оренбург' limit 1");
						$cities = $db->getTable("select * from `cities` where `name` not like 'Оренбург' order by `name`");
						echo "<option value='$oren'>Оренбург</option>";
						echo selectCreate($cities,"city_id","name",$joined['city']);
					?></select>
				</td>
				<td><label for="area">Район</label></td>
				<td>
					<select name='area' id='area'><?php
						$areas = $db->getTable("select * from `areas` where `city_id` = '".$joined['city']."' order by `name`");
						echo selectCreate($areas,"area_id","name",$joined['area']);
					?></select>
				</td>
			</tr>
			<tr>
				<td><label for="street">Улица</label></td>
				<td colspan='3'><input type='text' name='street' id='street' value='<?php echo $joined['street']; ?>' /></td>
			</tr>
			<tr>
				<td valign='top'><label for="comment">Ваш комментарий</label></td>
				<td colspan='3'><textarea name='comment' id='comment' style='width:100%' rows='5'><?php echo $joined['comment']; ?></textarea></td>
			</tr>
			<tr>
				<td colspan='4' align="right"><input type='submit' value="Редактировать" style='width:200px;height:35px' /></td>
			</tr>
		<table>
	</form><?php
}

require_once COORDINATOR.DS."footer.php";
?>